from django.contrib import admin

from oa.admin import AutoUpdateUserModelAdmin, SimpleModelAdmin
from .models import *


@admin.register(UserRelation)
class UserRelationAdmin(AutoUpdateUserModelAdmin):
    list_display = [
        'pk', 'leader', 'subordinate', 'min_amount',
        'created_by', 'updated_by', 'datetime_created', 'datetime_updated']
    list_display_links = ['pk', 'leader', 'subordinate']
    list_filter = ['leader', 'subordinate']
    autocomplete_fields = ['leader', 'subordinate']


@admin.register(Project)
class ProjectAdmin(AutoUpdateUserModelAdmin):
    list_display = [
        'pk', 'code', 'name', 'status', 'date_start', 'date_finish',
        'created_by', 'updated_by', 'datetime_created', 'datetime_updated']
    list_display_links = ['pk', 'code', 'name']
    search_fields = ['code', 'name']
    list_filter = ['status']
    filter_horizontal = ['related_person']


@admin.register(ApplicationType)
class ApplicationTypeAdmin(SimpleModelAdmin):
    list_display = ['pk', 'code', 'name', 'is_show', 'is_count', 'category']
    search_fields = ['code', 'name']
    list_filter = ['is_show', 'is_count', 'category']
    list_editable = ['category']


class ApplicationAttachmentInline(admin.TabularInline):
    model = ApplicationAttachment
    extra = 0
    fields = ['file']


class CostSplitInline(admin.TabularInline):
    model = CostSplit
    extra = 0
    fields = ['related_department', 'related_application_type', 'amount']
    autocomplete_fields = ['related_department']


@admin.register(Application)
class ApplicationAdmin(AutoUpdateUserModelAdmin):
    list_display = [
        'pk', 'code', 'name', 'related_project', 'related_application_type', 'status',
        'initial_amount', 'initial_notes', 'approval_amount', 'approval_notes',
        'created_by', 'updated_by', 'datetime_created', 'datetime_updated']
    list_display_links = ['pk', 'code', 'name']
    search_fields = ['code', 'name']
    list_filter = ['related_project', 'related_application_type', 'created_by']
    inlines = [ApplicationAttachmentInline, CostSplitInline]

    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        related_users_id_list = get_related_users(request, s='subordinates_and_own')
        queryset = queryset.filter(created_by_id__in=related_users_id_list)
        return queryset


@admin.register(ApprovalRecord)
class ApprovalRecordAdmin(SimpleModelAdmin):
    list_display = ['pk', 'related_application', 'pre_status', 'post_status', 'related_user']
    list_filter = ['pre_status', 'post_status', 'related_user']


@admin.register(MoneyFlow)
class MoneyFlowAdmin(SimpleModelAdmin):
    date_hierarchy = 'record_date'
    list_display = [
        'pk', 'record_code', 'application_type', 'record_date',
        'debit_side', 'credit_side', 'direction', 'cost_split_ls', 'record_notes']
    list_display_links = ['pk', 'record_code']
    list_filter = [
        'related_application_type', 'related_opposite_account', 'direction',
        'record_date', 'cost_split__related_department', 'related_application_type__category']
    search_fields = ['record_notes']
    inlines = [CostSplitInline]

    def create_cost_split(self, request, queryset):
        for mf in queryset:
            if mf.cost_split.count() == 0:
                CostSplit.objects.create(
                    related_money_flow_id=mf.pk,
                    record_date=mf.record_date,
                    amount=mf.debit_side,
                    related_application_type=mf.related_application_type
                )

    create_cost_split.short_description = "成本拆分"

    actions = [create_cost_split]


@admin.register(CostSplit)
class CostSplitAdmin(SimpleModelAdmin):
    date_hierarchy = 'record_date'
    list_display = [
        'pk', 'related_department',  'related_application_type', 'record_date',
        'amount', 'related_application', 'related_money_flow']
    list_filter = [
        'related_department', 'related_application_type', 'related_application_type__category', 'record_date']
    list_editable = ['related_department']
